package com.itheima.reggie.mappers;

import com.itheima.reggie.entity.AddressBook;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface AddressBookMapper {
    @Insert("insert into address_book values(null,#{userId},#{consignee}," +
                    "#{sex},#{phone},null,null,null,null,null,null," +
                    "#{detail},#{label},#{isDefault},#{createTime},#{updateTime}," +
                    "#{createUser},#{updateUser},0)")
    void save(AddressBook addressBook);

    @Select("select * from address_book where user_id=#{userId} order by update_time desc")
    List<AddressBook> findAll(Long userId);

    @Update("update address_book set is_default=0 where user_id=#{userId}")
    void closeAllDefault(Long userId);

    @Update("update address_book set is_default=1 where id=#{id}")
    void updateDefault(AddressBook addressBook);

    @Select("select * from address_book where id=#{id}")
    AddressBook findById(Long id);

    @Update("update address_book set consignee=#{consignee}," +
                    "sex=#{sex},phone=#{phone},detail=#{detail},label=#{label}," +
                    "update_time=#{updateTime},update_user=#{updateUser} where id=#{id}")
    void update(AddressBook addressBook);

    @Delete("delete from address_book where id=#{id}")
    void delete(Long id);

    @Select("select * from address_book where user_id=#{userId} and is_default=1")
    AddressBook getDefaultAddress(Long userId);
}
